package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.bus.AddShopEvent;
import cn.kooki.app.duobao.ui.listener.AddShopCartCallBack;
import cn.kooki.app.duobao.util.ToastUtils;
import cn.kooki.app.duobao.util.manager.ShopCartManager;


/**
 * Created by xiong on 2015/12/12.
 */
public class SearchDetailResultAdapter extends BaseAdapter {


    private AddShopCartCallBack cartCallBack;

    public void setCartCallBack(AddShopCartCallBack cartCallBack) {
        this.cartCallBack = cartCallBack;
    }

    private ArrayList<GoodsItem> datas;
    private Context context;

    public SearchDetailResultAdapter(Context context, ArrayList<GoodsItem> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final GoodsItem item = datas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_rect_goods_item, parent, false);
            viewHolder = new ViewHolder(convertView);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int currt = Integer.parseInt(item.getCanyurenshu());

        int total = Integer.parseInt(item.getZongrenshu());


        if (cartCallBack != null) {
            viewHolder.goodsAddCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    item.count = 1;
                    ShopCartManager.getInstance().addShopCart(item);
                    ToastUtils.showShortMessage("添加成功", context);

                }
            });
        }

        viewHolder.goodsProgress.setMax(total);
        viewHolder.goodsProgress.setProgress(currt);
        viewHolder.goodsName.setText(item.getTitle());
        viewHolder.goodsTotal.setText("总需" + item.getZongrenshu());
        viewHolder.goodsRemain.setText("剩余" + (total - currt));
        Glide.with(context).load(item.getThumb()).placeholder(R.drawable.img_blank).into(viewHolder.goodsImg);
        viewHolder.goodTenLabel.setVisibility(item.getYunjiage().equals("10") ? View.VISIBLE : View.GONE);
        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_rect_goods_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.goods_img)
        ImageView goodsImg;
        @Bind(R.id.goods_add_buy)
        TextView goodsAddBuy;
        @Bind(R.id.goods_add_cart)
        Button goodsAddCart;
        @Bind(R.id.goods_add)
        RelativeLayout goodsAdd;
        @Bind(R.id.goods_name)
        TextView goodsName;
        @Bind(R.id.goods_progress)
        ProgressBar goodsProgress;
        @Bind(R.id.goods_total)
        TextView goodsTotal;
        @Bind(R.id.goods_remain)
        TextView goodsRemain;
        @Bind(R.id.goods_free_label)
        ImageView goodsFreeLabel;
        @Bind(R.id.good_ten_label)
        ImageView goodTenLabel;
        @Bind(R.id.limit_duobao_id)
        ImageView limitDuobaoId;
        @Bind(R.id.custom_top_left_icon)
        ImageView customTopLeftIcon;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
